package net.chrisrichardson.cloudtools.maven.plugins;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.chrisrichardson.ec2deployer.cluster.ClusterManager;
import net.chrisrichardson.ec2deployer.cluster.SimpleCluster;


/**
 * Goal which runs a Jmeter test
 * 
 * @goal jmeter
 * 
 */
public class JmeterMojo extends AbstractClusterMojo {

  /**
   * Location of the Jmeter script.
   * 
   * @parameter expression="${cloudtools.jmxTestFile}"
   * 
   * @required
   */
  protected File jmxTestFile;
  
  /**
   * Thread count
   * 
   * @parameter expression="${cloudtools.threadCount}"
   * @required
   */
  protected String threadCount;


  /**
   * Data files
   * 
   * @parameter expression="${cloudtools.dataFiles}"
   */
  protected String dataFilesAsString;
  
  
  @Override
  protected void executeWithCluster(ClusterManager manager, SimpleCluster cluster) {
    List<Integer> threadCounts = new ArrayList<Integer>();
    String[] threadCountStrings = threadCount.split("[,]");
    for (String s : threadCountStrings) {
      threadCounts.add(Integer.parseInt(s));
    }
    List<String> dataFiles = new ArrayList<String>();
    if (dataFilesAsString != null) {
      dataFiles = Arrays.asList(dataFilesAsString.split("[,]"));
    }
    cluster.loadTest(jmxTestFile.getAbsolutePath().replace('\\', '/'), threadCounts, dataFiles);
  }
}
